App({
  globalData: {
    hotelName: ['宜尚酒店宁波联丰中路店'],
    hotel: 0,
    Url: 'https://jszxonline.xyz/hotel_ticket/',
    admin: null,     // 管理员标识
    user: {},     // 个人信息
    share: {},    // 分享信息
    receive: {},  // 领取信息
    showLog: 1,   // 调试开关
  },
  onLaunch: function() {
  },

  request: function(url, data, log) {
    var promise = new Promise((resolve, reject) => {
      var that = this, logStr = url.split('/').pop() + '.php: ';
      if (log) wx.showLoading({ title: log });
      log = log || 'Requesting';
      data.hotel = data.hotel || this.globalData.hotel;
      if (that.globalData.showLog) console.log(logStr + log + '...');
      wx.request({
        url: that.globalData.Url + url + '.php',
        data: data,
        success: function(res) {
          wx.hideLoading();
          res.name = logStr;
          if (res.statusCode == 200) resolve(res); 
          else reject(res);
        },
        fail: function(e) {
          wx.showToast({ icon: 'none', title: '网络连接失败' })
          reject('网络连接失败');
        }
      })
    });
    return promise;
  },

  modal: function (title, content, cancelText, confirmText, cancelColor, confirmColor) {
    var promise = new Promise((resolve, reject) => {
      wx.showModal({
        title: title || '提示',
        content: content,
        showCancel: cancelText || false,
        cancelText: cancelText || '取消',
        confirmText: confirmText || '好的',
        cancelColor: cancelColor || '',
        confirmColor: confirmColor || '',
        success: function (res) {
          if (res.confirm) resolve(res);
          else reject(res);
        },
        fail: function (res) {
          reject(res);
        },
      })
    });
    return promise;
  },
  
  toast: function (content, icon, mask) {
    var promise = new Promise((resolve, reject) => {
      wx.showToast({
        title: content,
        icon: icon || 'none',
        image: '',
        duration: 2000,
        mask: mask || false,
        success: function (res) { resolve(res)},
        fail: function (res) { reject(res)},
      })
    });
    return promise;
  },
})